package com.hjrpc.babyyamall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjrpc.babyyamall.product.dao.*;
import com.hjrpc.babyyamall.product.entity.*;
import com.hjrpc.babyyamall.product.service.AttrAttrgroupRelationService;
import com.hjrpc.babyyamall.product.service.AttrService;
import com.hjrpc.babyyamall.product.service.CategoryService;
import com.hjrpc.babyyamall.product.vo.AttrRespVo;
import com.hjrpc.babyyamall.product.vo.AttrVo;
import com.hjrpc.babyyamall.product.vo.SkuItemVo;
import com.hjrpc.common.utils.PageUtils;
import com.hjrpc.common.utils.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    private static final String BASE = "base";
    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductAttrValueDao productAttrValueDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils listWithType(Map<String, Object> params, String attrType, Long categoryId) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>();
        queryWrapper.eq("attr_type", attrType.equalsIgnoreCase(BASE) ? 1 : 0);
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.eq("attr_id", key).or().like("attr_name", key);
        }
        if (categoryId != null && categoryId != 0) {
            queryWrapper.eq("catelog_id", categoryId);
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params), queryWrapper
        );
        //查询出所属分类和所属分组
        List<AttrRespVo> attrRespVos = page.getRecords().stream().map(this::queryAttrRespVo)
                .collect(Collectors.toList());

        //将值封装到页面分页对象中
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(attrRespVos);
        return pageUtils;
    }

    private AttrRespVo queryAttrRespVo(AttrEntity x) {
        //复制基础属性
        AttrRespVo respVo = new AttrRespVo();
        BeanUtils.copyProperties(x, respVo);

        //根据属性查询对应的分组名称
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao
                .selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .eq("attr_id", x.getAttrId()));
        //注意:销售属性没有分组信息
        if (attrAttrgroupRelationEntity != null) {
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
            respVo.setAttrGroupId(attrGroupEntity.getAttrGroupId());
            respVo.setGroupName(attrGroupEntity.getAttrGroupName());
        }

        //根据分类信息查询分类名称
        CategoryEntity categoryEntity = categoryDao.selectById(x.getCatelogId());
        respVo.setCatelogName(categoryEntity.getName());
        //根据分类Id组装给前端显示的数组
        respVo.setCatelogPath(categoryService.findCatelogPath(x.getCatelogId()));
        return respVo;
    }

    @Override
    public void saveWithGroup(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo, attrEntity);
        //保存属性的基础信息
        this.baseMapper.insert(attrEntity);

        //保存属性的分组信息
        Long attrGroupId = attrVo.getAttrGroupId();
        if (attrGroupId != null && attrGroupId != 0) {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(attrEntity.getAttrId());
            relationEntity.setAttrGroupId(attrGroupId);
            attrAttrgroupRelationDao.insert(relationEntity);
        }
    }

    @Override
    public AttrRespVo getAttrDetail(Long attrId) {
        AttrEntity attrEntity = this.baseMapper.selectById(attrId);
        return queryAttrRespVo(attrEntity);
    }

    @Override
    public void updateDetail(AttrVo attrVo) {
        AttrEntity entity = new AttrEntity();
        BeanUtils.copyProperties(attrVo, entity);
        this.baseMapper.updateById(entity);

        //判断是否修改过分组信息
        Long attrGroupId = attrVo.getAttrGroupId();
        if (attrGroupId != null && attrGroupId != 0) {
            UpdateWrapper<AttrAttrgroupRelationEntity> wrapper = new UpdateWrapper<>();
            wrapper.set("attr_group_id", attrGroupId);
            wrapper.eq("attr_id", entity.getAttrId());
            attrAttrgroupRelationService.update(wrapper);
        }
    }

    @Override
    public List<ProductAttrValueEntity> list4Spu(Long spuId) {
        return productAttrValueDao.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
    }

    @Override
    public List<SkuItemVo.SpuItemSaleAttr> querySkuSaleInfo(Long spuId) {
        return this.baseMapper.querySkuSaleInfo(spuId);
    }
}